﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.SysConfig;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller.System
{
    /// <summary>
    /// 系统配置
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21 
    /// </summary>
    [Route("api/System/[controller]")]
    public class SysConfigController : BaseApiController
    {
        private SysConfigBll sysConfigBll = new SysConfigBll();

        #region GET
        /// <summary>
        /// 获取系统配置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetInfo()
        {
            var array = new Dictionary<string, string>();
            var data = (await sysConfigBll.GetList()).FindAll(x => x.Category.Equals("SysConfig"));
            foreach (var item in data)
            {
                array.Add(item.Key, item.Value);
            }
            var output = array.ToJson().ToObject<SysConfigOutput>();
            return Success(output);
        }
        #endregion

        #region Post
        /// <summary>
        /// 邮箱链接测试
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("Email/Test")]
        public async Task<ResponseResult> EmailTest([FromBody] SysConfigEmailTestInput input)
        {
            var emailConfigEntity = input.MapTo<EmailConfigEntity>();
            var mailAccount = emailConfigEntity.MapTo<MailAccount>();
            if ("1".Equals(emailConfigEntity.Ssl))
            {
                mailAccount.Ssl = true;
            }
            else
            {
                mailAccount.Ssl = false;
            }
            var result = MailHelper.CheckConnected(mailAccount);
            if (result)
            {
                return Success("验证成功");
            }
            else
            {
                return Error("账户认证错误");
            }
        }

        /// <summary>
        /// 更新系统配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResponseResult> Update([FromBody] SysConfigUpInput input)
        {
            string configJson = JsonHelper.ToJson(input);
            var entitys = new List<SysConfigEntity>();
            var array = configJson.ToObject();
            foreach (var item in array)
            {
                SysConfigEntity sysConfigEntity = new SysConfigEntity();
                sysConfigEntity.Id = CommonHelper.Guid;
                sysConfigEntity.Key = item.Key;
                sysConfigEntity.Value = item.Value.ToString();
                sysConfigEntity.Category = "SysConfig";
                entitys.Add(sysConfigEntity);
            }
            await sysConfigBll.Save(entitys, "SysConfig");
            return Success("操作成功");
        }
        #endregion
    }
}
